/**
 * 系统模块查看js
 */
var sysModuleLook={
	dataCache:null,
		
	startEvent:function(){
		
		//获取所有资源
		var url ='mgr/SysResourceCtrl/getSysResourceList';
		var params={
			'curPage':0,
			'pageSize':9999
		};
		var cb = function(data){
			sysModuleLook.dataCache=data.result;
		};
		sendApiNoLoadSync(url, params, cb, 'POST');
		sysModuleLook.showInfoById();
	},
	
	showInfoById:function(){
		var lokId=art.dialog.data('lokId');  //查看id
		var url = 'mgr/SysModuleCtrl/getSysModuleById';
		var params ={
				'id':lokId,
				'curPage':0,
				'pageSize':9999
		};
		var cb = function(data){
			if(data.resultCode==200){
				var ret=data.result.sysModule;
				for(var i in ret){
					var text=ret[i];
					$('#com-addForm td[name="'+i+'"]').html(text);
				}
				var resourceId='';
				var resourceCache=data.result.sysModuleResources;
				
				for(var j=0;j<resourceCache.length;j++){
					resourceId=resourceId+resourceCache[j].resourceId+',';
				}
				if(resourceId!=null&&resourceId!=''){
					resourceId=resourceId.substring(0,resourceId.length-1);
					$('#com-addForm input[name="resourceIds"]').val(resourceId);
				}
				sysModuleLook.showTreeData();
			}
		};
		sendApiNoLoad(url, params, cb, 'POST');
		//sysModuleLook.showInfoById();
	},
	
	setCheck:function(){
		var zTree = $.fn.zTree.getZTreeObj("selSysResourcesTree"),
		py = "",
		sy = "s",
		pn = "",
		sn = "s",
		type = { "Y":py + sy, "N":pn + sn};
		zTree.setting.check.chkboxType = type;
	},
	
	showTreeData:function(){
		var setting = {
			check: {
				enable: true
			},
			data: {
				simpleData: {
					enable: true
				}
			},
			view: {
				nameIsHTML: true
			}
		};
		var zNodes = [];
		var dataCache=sysModuleLook.dataCache;
		var resourceIds=$('#com-addForm input[name="resourceIds"]').val();
		for (var i = 0; i < dataCache.length; i++) {
			zNodes[i]={
				id:dataCache[i].id,
				name:dataCache[i].name,
				pId:dataCache[i].parentId
			};
			if(i==0){
				zNodes[i].open=true;
			}
			if(resourceIds!=null){
				var selResourceIds=resourceIds.split(',');
				for(var j=0;j<selResourceIds.length;j++){
					if(zNodes[i].id==selResourceIds[j]){
						zNodes[i].checked=true;
						continue;
					}
				}
			}
		}
		$.fn.zTree.init($("#selSysResourcesTree"), setting, zNodes);
		sysModuleLook.setCheck();
		$("#py").bind("change", sysModuleLook.setCheck());
		$("#sy").bind("change", sysModuleLook.setCheck());
		$("#pn").bind("change",	sysModuleLook.setCheck());
		$("#sn").bind("change",	sysModuleLook.setCheck());
	},
	
	init:function(){
		sysModuleLook.startEvent();
	}
}
$(document).ready(function() {
	sysModuleLook.init();
});